package com.desire.config.autoconfig;

import com.desire.config.TokenInterceptorRegistry;
import com.desire.interceptor.TokenInterceptor;
import com.desire.utils.TokenUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-09 周一 14:39
 * @since 17
 */
@Configuration(
        proxyBeanMethods = false
)
@EnableConfigurationProperties(TokenProperties.class)
@ConditionalOnProperty(
        prefix = "in.token",
        value = "enabled",
        havingValue = "true",
        matchIfMissing = true
)
public class TokenAutoConfiguration {

    TokenProperties properties;

    public TokenAutoConfiguration(TokenProperties tokenProperties) {
        this.properties = tokenProperties;
    }

    /**
     * 向 IOC 容器注入拦截器
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public TokenInterceptorRegistry tokenInterceptorRegistry() {
        return new TokenInterceptorRegistry();
    }

    @Bean
    public TokenInterceptor tokenInterceptor() {
        return new TokenInterceptor();
    }

    @Bean
    @ConditionalOnMissingBean
    public TokenUtils tokenUtils() {
        return new TokenUtils();
    }
}
